layui.use(['form', 'layer', 'element', 'laydate', 'table'], function () {
    var form = layui.form;
    var layer = layui.layer;
    var element = layui.element;
    var laydate = layui.laydate;
    var table = layui.table;


    form.on('submit(as)', function (data) {
        data.field.note = $("#note").val();
        if (data.field.status != 'on') {
            data.field.status = 'off';
        }
        $.ajax({
            type: 'post',
            url: '/customer/addCustomer',
            contentType: 'application/json; charset=utf-8',
            data: JSON.stringify(data.field),
            success: function (result) {
                if (result == "success") {
                    layer.msg("添加成功！", {icon: 1}, function (index) {
                        location.reload();
                    });
                } else if (result == "fail") {
                    layer.msg("添加失败！", {icon: 2}, function (index) {
                        layer.close();
                    });
                }
            },
            error: function (error) {
                layer.msg('error');
            }
        });
        return false;
    });
    //table客户信息获取
    var table2 = table.render({
        elem: "#table2",
        url: "/customer/getCustomerInfo",
        method: 'post',
        cols: [[
            {field: 'id', title: 'ID', align: 'center', width: '5%', type: 'numbers'},
            {field: 'name', title: '姓名', align: 'center', width: '15%'},
            {field: 'sex', title: '性别', align: 'center', width: '5%'},
            {field: 'phone', title: '电话', align: 'center', width: '20%'},
            {field: 'note', title: '备注', align: 'center', width: '35%'},
            {
                field: 'status', title: '状态', align: 'center', width: '10%', templet:
                    function (row) {
                        var html = '<div class="layui-input-block" style="margin-left: 0px;">' +
                            '       <input type="checkbox" name="status" lay-text="On|Off" lay-skin="switch" disabled ';
                        if (row.status == "on") {
                            html += 'checked';
                        }
                        html += '></div>';
                        return html;
                    }
            },
            {field: 'action1', title: '操作', align: 'center', toolbar: '#bar2', width: '10%'}
        ]],
        page: true
    });
    //编辑,删除表格行数据
    table.on('tool(table2)', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;
        if (layEvent == "edit") {
            layer.open({
                type: 2,
                title: '修改客户信息',
                shadeClose: false,
                shade: 0.8,
                maxmin: true,
                area: ['60%', '60%'],
                content: '/customer/toEditCustomer?id=' + data.id
            });
        }
        if (layEvent=='del'){
            layer.confirm('您确定要删除?',function (index) {
                $.ajax({
                    type:'post',
                    url:'/customer/delCustomerInfo',
                    data:{
                        "id":data.id
                    },
                    success:function (result) {
                        if (result=="success"){
                            layer.msg("删除成功！",{icon:1,time:1000},function (index) {
                                layer.close(index);
                                location.reload();
                            });
                        }
                        if (result=="fail"){
                            layer.msg("删除失败！",{icon:2,time:1000},function (index) {
                                layer.close(index);
                            });
                        }
                        if (result=="infoError"){
                            layer.msg("删除失败！",{icon:2,time:1000},function (index) {
                                layer.close(index);
                            });
                        }
                    },
                    error:function (error) {
                        layer.msg("404");
                    }
                });
            });
        }
    });
    form.on('submit(edit_submit)', function (data) {
        console.log(data.field);
        var note = $("#note").val();
        data.field.note = note;
        $.ajax({
            type: 'post',
            url: '/customer/editCustomerInfo',
            contentType: 'application/json;charset=utf-8',
            data: JSON.stringify(data.field),
            success:function (result) {
                if (result=="success"){
                    layer.alert("修改成功！",{icon:1},function (index) {
                        layer.close(index);
                        var pIndex = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(pIndex);
                        parent.location.reload();
                    });
                }
                if (result=="fail"){
                    layer.alert("修改失败，请重试！",{icon:2},function (index) {
                        layer.close(index);
                    });
                }
            },
            error:function (error) {
                layer.msg("404,error");
            }
        });
        return false;
    })
});